package com.youkeyi.ddy.cloud.application.business.crm.inputConfig.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.crm.config.BusinessCustomerInputConfigCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.config.dto.BusinessCustomerInputConfigListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.config.dto.BusinessCustomerInputConfigListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.config.param.BusinessCustomerInputConfigCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.config.param.BusinessCustomerInputConfigDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.config.param.BusinessCustomerInputConfigListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.config.param.BusinessCustomerInputConfigUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.CrmCustomerInputConfigCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.dto.CrmCustomerInputConfigListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.param.CrmCustomerInputConfigCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.param.CrmCustomerInputConfigDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.param.CrmCustomerInputConfigQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.param.CrmCustomerInputConfigUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * BusinessCustomerInputConfigController
 *
 * @Author liuxiawang
 * @Date 2024/3/1 11:52 AM
 * @Location shenzhen.china
 */
@Service
public class BusinessCustomerInputConfigController implements BusinessCustomerInputConfigCloudService {

    @Autowired
    private CrmCustomerInputConfigCloudService crmCustomerInputConfigCloudService;

    @Override
    public BusinessCustomerInputConfigListCloudDto list(BusinessCustomerInputConfigListCloudParam param) {
        BusinessCustomerInputConfigListCloudDto dto = new BusinessCustomerInputConfigListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        CrmCustomerInputConfigQueryCloudParam configQueryCloudParam = new CrmCustomerInputConfigQueryCloudParam();
        configQueryCloudParam.setCustomerInputConfigPrimaryKeys(param.getCustomerInputConfigPrimaryKeys());
        configQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        configQueryCloudParam.setDisplayFieldsFuzzy(param.getDisplayFieldsFuzzy());
        configQueryCloudParam.setPageSize(param.getPageSize());
        configQueryCloudParam.setCurrentPage(param.getCurrentPage());
        CrmCustomerInputConfigListCloudDto configDtos = crmCustomerInputConfigCloudService.query(configQueryCloudParam);
        if (null != configDtos && CollectionUtils.isNotEmpty(configDtos.getRows())) {
            List<BusinessCustomerInputConfigListItemCloudDto> items = new ArrayList<>();
            configDtos.getRows().forEach(config -> {
                BusinessCustomerInputConfigListItemCloudDto item = new BusinessCustomerInputConfigListItemCloudDto();
                item.setCustomerInputConfigPrimaryKey(config.getCustomerInputConfigPrimaryKey());
                item.setDisplayFields(config.getDisplayFields());
                item.setIsDeleted(config.getIsDeleted());
                item.setIsDeletedName(config.getIsDeletedName());
                item.setCreateUserKey(config.getCreateUserKey());
                item.setCreateUserName(config.getCreateUserName());
                item.setCreateTime(config.getCreateTime());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(configDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessCustomerInputConfigCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getDisplayFields())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerInputConfigCreateCloudParam configCreateCloudParam = new CrmCustomerInputConfigCreateCloudParam();
        configCreateCloudParam.setCorpKey(corpKey);
        configCreateCloudParam.setDisplayFields(param.getDisplayFields());
        configCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        configCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return crmCustomerInputConfigCloudService.create(configCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessCustomerInputConfigUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getCustomerInputConfigPrimaryKey())
                || StringUtils.isBlank(param.getDisplayFields())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerInputConfigUpdateCloudParam configUpdateCloudParam = new CrmCustomerInputConfigUpdateCloudParam();
        configUpdateCloudParam.setCustomerInputConfigPrimaryKey(param.getCustomerInputConfigPrimaryKey());
        configUpdateCloudParam.setCorpKey(corpKey);
        configUpdateCloudParam.setDisplayFields(param.getDisplayFields());
        configUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        configUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmCustomerInputConfigCloudService.update(configUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessCustomerInputConfigDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getCustomerInputConfigPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerInputConfigDeleteCloudParam configDeleteCloudParam = new CrmCustomerInputConfigDeleteCloudParam();
        configDeleteCloudParam.setCustomerInputConfigPrimaryKeys(param.getCustomerInputConfigPrimaryKeys());
        configDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        configDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmCustomerInputConfigCloudService.delete(configDeleteCloudParam);
    }
}
